<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Validator;
use Session;
use DB;
class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        //
        //captcha 验证码validate验证
        Validator::extend('captcha', function($attribute, $value, $parameters) {
            if ( Session::get('adminValiCode') == $value) {
                return true;
            } else {
                return false;
            }
        });

        Validator::extend('multi_unique', function($attribute, $value, $parameters, $validator) {
            $value2 = request($parameters[0]);
            $int_Count = DB::table($parameters[1])
                ->where($parameters[2],$value)
                ->where($parameters[3],$value2)
                ->count();
            return $int_Count == 0;
        });

        Validator::replacer('multi_unique',function($message, $attribute, $rule, $parameters){
            return sprintf('%s - %s 组合已经存在！',$attribute,$parameters[0]);
        });

        Validator::extend('phone', function($attribute, $value, $parameters, $validator) {
            return is_phone($value);
        });
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->singleton('CebApiRequest',function(){
            return new \App\Components\CebApiRequest();
        });
    }
}
